#! /usr/bin/env bash
#
# git-radar
#
# A heads up display for git

if [[ "$OSTYPE" == *darwin* ]]; then
  READLINK_CMD='greadlink'
else
  READLINK_CMD='readlink'
fi

dot="$(cd "$(dirname "$([ -L "$0" ] && $READLINK_CMD -f "$0" || echo "$0")")"; pwd)"
args=$@

if [[ -z $@ ]]; then
  _git="\033[1;30mgit:(\033[0m"
  _master="\033[0;37mmaster\033[0m"
  _my_branch="\033[0;37mmy-branch\033[0m"
  _endgit="\033[1;30m)\033[0m"
  _untracked="\033[1;37mA\033[0m"
  _added_staged="\033[1;32mA\033[0m"
  _modified_unstaged="\033[1;31mM\033[0m"
  _local_up="\033[1;32m↑\033[0m"
  _2_from_master="\xF0\x9D\x98\xAE 2 \033[1;31m→\033[0m "
  _diverged_from_master="\xF0\x9D\x98\xAE 2 \033[1;33m⇄\033[0m 3 "
  _not_upstream="upstream \033[1;31m⚡\033[0m "
  _detached="\033[0;37mdetached@94eac67\033[0m"
  _conflicted_us="\033[1;33mU\033[0m"
  _conflicted_them="\033[1;33mT\033[0m"
  _ahead_master="\xF0\x9D\x98\xAE \033[1;32m←\033[0m"
  _local_diverged="\033[1;33m⇵\033[0m"
  echo "git-radar - a heads up display for git"
  echo ""
  echo "examples:"
  printf "  $_git$_master$_endgit"
  echo "                  # You are on the master branch and everything is clean"
  printf "  $_git$_not_upstream$_my_branch$_endgit"
  echo "    # Fresh branch that we haven't pushed upstream"
  printf "  $_git$_my_branch$_endgit 2$_untracked"
  echo "            # Two files created that aren't tracked by git"
  printf "  $_git$_my_branch$_endgit 1$_added_staged 3$_modified_unstaged"
  echo "         # 1 new file staged to commit and 3 modifications that we still need to \`git add\`"
  printf "  $_git$_2_from_master$_my_branch 3$_local_up$_endgit"
  echo "      # 3 commits made locally ready to push up while master is ahead of us by 2"
  printf "  $_git$_diverged_from_master$_my_branch$_endgit"
  echo "       # our commits pushed up, master and my-branch have diverged"
  printf "  $_git$_detached$_endgit 2${_conflicted_them}3${_conflicted_us}"
  echo "   # mid rebase, we are detached and have 3 conflicts caused by US and 2 caused by THEM"
  printf "  $_git$_diverged_from_master$_my_branch 3${_local_diverged}5$_endgit"
  echo "   # rebase complete, our rewritten commits now need pushed up"
  printf "  $_git$_ahead_master 3 $_my_branch$_endgit"
  echo "         # origin/my-branch is up to date with master and has our 3 commits waiting merge"

  echo ""
  echo "usage:"
  echo "  git-radar [--zsh|--bash|--fish] [--fetch]"
  echo ""
  echo "  --fetch  # Fetches your repo asynchronously in the background every 5 mins"
  echo "  --zsh    # Output prompt using Zsh style color characters"
  echo "  --bash   # Output prompt using Bash style color characters"
  echo "  --fish   # Output prompt using fish style color characters"
  echo ""
  echo "Bash example:"
  echo "  export PS1=\"\\W\\\$(git-radar --bash --fetch) \""
  echo ""
  echo "  This will show your current directory and the full git-radar."
  echo "  As an added benefit, if you are in a repo, it will asynchronously"
  echo "  run \`git fetch\` every 5 mins, so that you are never out of date."
  echo ""
  echo "Zsh example:"
  echo "  export PROMPT=\"%1/%\\\$(git-radar --zsh --fetch) \""
  echo ""
  echo "  Same as the Bash but for Zsh."
  echo ""
  echo "fish example:"
  echo "  function fish_prompt"
  echo "    set_color \$fish_color_cwd"
  echo "    echo -n (prompt_pwd)"
  echo "    git-radar --fish -fetch"
  echo "    set_color normal"
  echo "    echo -n ' > '"
  echo "  end"
  echo ""
  echo "  Same as the Bash but for fish."
  exit
fi
while [[ $# > 0 ]];do
  command="$1"
  shift

  if [[ "$command" == "--fetch" ]]; then
    nohup $dot/fetch.sh >/dev/null 2>&1 &
  fi
  if [[ "$command" == "--zsh" ]]; then
    $dot/prompt.zsh $args
  fi
  if [[ "$command" == "--bash" || "$command" == "--fish" ]]; then
    $dot/prompt.bash $args
  fi
done
